package com.dominator.repository;

import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;

import com.dominFramework.core.id.SystemIdUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.mybatis.dao.T_accountDao;
import com.dominator.mybatis.dao.T_accountPO;
import com.dominator.mybatis.dao.T_userPO;
import com.dominator.utils.dao.JdbcHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dominFramework.core.dao.SqlDao;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;

import com.dominator.utils.exception.ApiException;

@Repository
public class DaoAccount {
	@Resource
	JdbcHelper jh;

	@Autowired
	T_accountDao accountDao;

	@Autowired
	SqlDao sqlDao;

	/**
	 * 添加
	 * 
	 * @param record
	 * @return
	 */
	public Integer insertSelective(T_accountPO record) {
		return accountDao.insert(record);
	}

	/**
	 * 删除
	 * 
	 * @param
	 * @return
	 */
	public Integer deleteByPrimaryKey(String id) {
		return accountDao.deleteByKey(id);
	}

	/**
	 * 修改
	 * 
	 * @param record
	 * @return
	 */
	public Integer updateByPrimaryKey(T_accountPO record) {
		return accountDao.updateByKey(record);
	}

	/**
	 * 查询单条记录
	 * 
	 * @param id
	 * @return
	 */
	public T_accountPO selectByPrimaryKey(String id) {
		T_accountPO row = accountDao.selectByKey(id);
		return row;
	}

	/**
	 * 通过用户id查询账户
	 * 
	 * @param
	 * @return
	 */
	public T_accountPO selectByUserId(String userId) {
		Dto qDto = Dtos.newDto("userId", userId);
		T_accountPO row = (T_accountPO) sqlDao.selectOne("Account.selectByUserId", qDto);
		return row;
	}

	/**
	 * 添加账户
	 * 
	 * @param
	 * @return
	 */
	public Integer createAccount(T_userPO userPO) {
		T_accountPO account = new T_accountPO();
		account.setId(SystemIdUtils.uuid());
		account.setUser_id(userPO.getId());
		account.setPassword(userPO.getPassword());
		account.setPoints(BigDecimal.ZERO);
		account.setMoney(BigDecimal.ZERO);
		account.setCreate_time(new Date());
		account.setIs_valid("1");
		return accountDao.insert(account);
	}
	
	
	/**
	 * 充值
	 * @param userId
	 * @return
	 */
	public Integer addMoney(String userId, BigDecimal money) {
		T_accountPO account = selectByUserId(userId);	
		account.setMoney(account.getMoney().add(money));
		return accountDao.updateByKey(account);
	}


	/**
	 * 充值
	 * @param userId
	 * @return
	 */
	public Integer addPoints(String userId, BigDecimal points) {
		T_accountPO account = selectByUserId(userId);
		account.setPoints(account.getPoints().add(points));
		return accountDao.updateByKey(account);
	}





	/**
	 * 扣费
	 * @param userId
	 * @return
	 */
	public Integer subtract(String userId, BigDecimal money) throws ApiException{
		T_accountPO account = selectByUserId(userId);	
		if(account.getMoney().subtract(money).compareTo(new BigDecimal(0))<0){
			throw new ApiException(ReqEnums.ACC_LACK_OF_BALANCE);
		}
		account.setMoney(account.getMoney().subtract(money));
		return accountDao.updateByKey(account);
	}
	
	

	
	
	
	
	

}
